/*    */ package cc.s2m.web.s2mBlog.interceptors;
/*    */ 
/*    */ import cc.s2m.util.CookieUtil;
/*    */ import cc.s2m.web.s2mBlog.util.MemcacheKeys;
/*    */ import cc.s2m.web.s2mBlog.util.StaticProp;
/*    */ import java.io.PrintWriter;
/*    */ import java.util.Enumeration;
/*    */ import java.util.UUID;
/*    */ import javax.servlet.http.HttpServletRequest;
/*    */ import javax.servlet.http.HttpServletResponse;
/*    */ import javax.servlet.http.HttpSession;
/*    */ import net.spy.memcached.MemcachedClient;
/*    */ import org.springframework.beans.factory.annotation.Autowired;
/*    */ import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
/*    */ 
/*    */ public class TokenInterceptor extends HandlerInterceptorAdapter
/*    */ {
/*    */ 
/*    */   @Autowired
/*    */   private MemcachedClient memcachedClient;
/*    */ 
/*    */   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
/*    */     throws Exception
/*    */   {
/* 27 */     if (request.getRequestURI().equals("/code")) {
/* 28 */       return super.preHandle(request, response, handler);
/*    */     }
/* 30 */     if (request.getRequestURI().equals("/umUpload")) {
/* 31 */       return super.preHandle(request, response, handler);
/*    */     }
/* 33 */     if (request.getRequestURI().equals("/weixin")) {
/* 34 */       return super.preHandle(request, response, handler);
/*    */     }
/*    */ 
/* 37 */     String token = getTokenValue(request, response);
/* 38 */     if (("POST".equalsIgnoreCase(request.getMethod())) && (request.getParameterNames().hasMoreElements()))
/*    */     {
/* 40 */       String token_r = request.getParameter("token");
/* 41 */       if ((token_r == null) || (token_r.trim().isEmpty())) {
/* 42 */         response.setContentType("text/html; charset=utf-8");
/* 43 */         PrintWriter out = response.getWriter();
/* 44 */         out.println("请求已过期，请返回刷新页面重试！");
/* 45 */         out.flush();
/* 46 */         out.close();
/* 47 */         return false;
/*    */       }
/* 49 */       if (!token_r.equals(token)) {
/* 50 */         response.setContentType("text/html; charset=utf-8");
/* 51 */         PrintWriter out = response.getWriter();
/* 52 */         out.println("请求已过期，请返回刷新页面重试！");
/* 53 */         out.flush();
/* 54 */         out.close();
/* 55 */         return false;
/*    */       }
/*    */ 
/*    */     }
/*    */ 
/* 61 */     return super.preHandle(request, response, handler);
/*    */   }
/*    */ 
/*    */   public String getTokenValue(HttpServletRequest request, HttpServletResponse response)
/*    */   {
/* 74 */     CookieUtil cookie = new CookieUtil(request, response);
/* 75 */     String cookieId = cookie.getCookie(StaticProp.cookieID);
/* 76 */     if ((cookieId == null) || (cookieId.trim().isEmpty())) {
/* 77 */       cookieId = request.getSession(true).getId();
/*    */     }
/* 79 */     String token = (String)this.memcachedClient.get(MemcacheKeys.CSRF_TOKEN.getKey() + cookieId);
/* 80 */     if ((token == null) || (token.trim().isEmpty())) {
/* 81 */       token = UUID.randomUUID().toString();
/* 82 */       this.memcachedClient.set(MemcacheKeys.CSRF_TOKEN.getKey() + cookieId, 1800, token);
/*    */     }
/* 84 */     request.setAttribute("token", token);
/* 85 */     return token;
/*    */   }
/*    */ }

/* Location:           C:\Users\Administrator\Desktop\s2mBlog.zip
 * Qualified Name:     s2mBlog-3.0.0.WEB-INF.classes.cc.s2m.web.s2mBlog.interceptors.TokenInterceptor
 * JD-Core Version:    0.6.1
 */